# -*- coding: utf-8 -*-
import maya.cmds as cmds
import maya.mel as mel


class Check_TPose ( ) :

    def __init__ ( self ) :
        self.starTime = 101
        self.copyTime = 90
        self.copyToTime = 70
        self.TopesTime = 50
        self.RootX_M = [ "RootX_M" ]
        self.IK_Limbs = [ "IKLeg_R" , "IKLeg_L" , "IKArm_R" , "IKArm_L" ]
        self.IK_PoleArms = [ "PoleArm_R" , "PoleArm_L" , "PoleLeg_R" , "PoleLeg_L" ]
        self.FK_Limbs_Globals = [ "FKShoulder_R" , "FKShoulder_L" , "FKHip_R" , "FKHip_L" ]

        self.Attributes = [ "translateX" , "translateY" , "translateZ" , "rotateX" , "rotateY" , "rotateZ" ]
        self.KeyCvs = [ "TheFly" , "FKEye_R" , "FKEye_L" , "FKIKSpline_M" , "FKIKArm_R" , "FKIKArm_L" , "FKIKLeg_R" ,
                        "FKIKLeg_L" , "FKIKSpine_M" , "FKTF_joint2_M" , "FKTF_joint3_M" , "FKTF_joint4_M" ,
                        "FKTF_joint5_M" , "FKPinkyFinger1_L" , "FKPinkyFinger5_L" , "FKCup_L" , "FKThumbFinger3_L" ,
                        "FKRingFinger2_L" , "FKRingFinger1_L" , "FKRingFinger5_L" , "FKPinkyFinger3_L" ,
                        "FKPinkyFinger2_L" , "FKSpine1_M" , "FKRootPart1_M" , "FKThumbFinger2_L" , "FKThumbFinger1_L" ,
                        "FKChest_M" , "FKNeck_M" , "FKScapula_R" , "HipSwinger_M" , "FKRoot_M" , "FKScapula_L" ,
                        "FKHead_M" , "FKXiuZi03_R" , "FKXiuZi02_R" , "FKXiuZi01_R" , "FKWrist_R" , "FKElbow_R" ,
                        "FKShoulder_R" , "FKIndexFinger3_R" , "FKIndexFinger2_R" , "FKIndexFinger1_R" ,
                        "FKIndexFinger5_R" , "FKRingFinger3_R" , "FKRingFinger2_R" , "FKRingFinger1_R" ,
                        "FKRingFinger5_R" , "FKMiddleFinger3_R" , "FKMiddleFinger2_R" , "FKMiddleFinger1_R" ,
                        "FKMiddleFinger5_R" , "FKPinkyFinger3_R" , "FKPinkyFinger2_R" , "FKPinkyFinger1_R" ,
                        "FKThumbFinger1_R" , "FKToes_L" , "FKAnkle_L" , "FKPinkyFinger5_R" , "FKCup_R" ,
                        "FKThumbFinger3_R" , "FKThumbFinger2_R" , "FKToes_R" , "FKAnkle_R" , "FKKnee_R" , "FKHip_R" ,
                        "FKKnee_L" , "FKHip_L" , "ctrlEmotions_M" , "ctrlPhonemes_M" , "ctrlMouthCorner_L" ,
                        "ctrlMouthCorner_R" , "ctrlMouth_M" , "ctrlNose_L" , "ctrlNose_R" , "ctrlCheek_L" ,
                        "ctrlCheek_R" , "ctrlEye_L" , "Jaw_M" , "SmilePull_L" , "SmilePull_R" , "lowerFace_M" ,
                        "Nose_M" , "ctrlEye_R" , "ctrlBrow_L" , "ctrlBrow_R" , "ctrlBox" , "upperTeeth_M" ,
                        "upperLidBase_L" , "EyeBrowOuter_L" , "EyeBrowInner_L" , "Pupil_L" , "CheekRaiser_L" ,
                        "CheekRaiser_R" , "EyeBrowCenter_M" , "Iris_L" , "Eye_L" , "lowerLid_L" , "upperLid_L" ,
                        "outerLid_L" , "Pupil_R" , "Iris_R" , "Eye_R" , "lowerLid_R" , "innerLid_L" , "upperLidBase_R" ,
                        "EyeBrowOuter_R" , "EyeBrowInner_R" , "Tongue3_M" , "Tongue2_M" , "Tongue1_M" , "Tongue0_M" ,
                        "lowerTeeth_M" , "upperLid_R" , "outerLid_R" , "innerLid_R" , "upperLipA_L" , "lowerLipB_R" ,
                        "lowerLipA_R" , "lowerLipB_L" , "lowerLipA_L" , "upperLipB_L" , "Lip_R" , "lowerLip_M" ,
                        "upperLipB_R" , "upperLipA_R" , "Lip_L" , "upperLip_M" , "NoseCorner_L" , "Cheek_L" ,
                        "FrownBulge_L" , "SmileBulge_L" , "EyeBrowMid3_L" , "EyeBrowMid2_L" , "EyeBrowMid1_L" ,
                        "lowerOuterLid_L" , "Cheek_R" , "FrownBulge_R" , "SmileBulge_R" , "EyeBrowMid3_R" ,
                        "EyeBrowMid2_R" , "lowerInnerLid_L" , "upperOuterLid_L" , "upperInnerLid_L" , "NoseCorner_R" ,
                        "EyeBrowMid1_R" , "lowerOuterLid_R" , "lowerInnerLid_R" , "upperOuterLid_R" ,
                        "upperInnerLid_R" , "AimEye_L" , "AimEye_R" , "AimEye_M" , "Fingers_L" , "Fingers_R" ,
                        "RootX_M" , "PoleArm_L" , "IKLocalArm_L" , "RollToesEnd_L" , "RollHeel_L" , "IKLeg_L" ,
                        "IKArm_L" , "PoleLeg_L" , "IKToes_L" , "RollToes_L" , "PoleArm_R" , "IKLocalArm_R" , "IKArm_R" ,
                        "IKLeg_R" , "PoleLeg_R" , "IKToes_R" , "RollToes_R" , "RollToesEnd_R" , "RollHeel_R" ,
                        "FKWrist_L" , "FKElbow_L" , "FKShoulder_L" , "FKIndexFinger2_L" , "FKIndexFinger1_L" ,
                        "FKIndexFinger5_L" , "FKMiddleFinger3_L" , "FKXiuZi03_L" , "FKXiuZi02_L" , "FKXiuZi01_L" ,
                        "FKIndexFinger3_L" , "FKMiddleFinger2_L" , "FKMiddleFinger1_L" , "FKMiddleFinger5_L" ,
                        "FKRingFinger3_L" ]

    # self.MergeAnim_layers()

    '''
	def MergeAnim_layers(self):
		layers_str = ''
		anim_layers = cmds.ls(type="animLayer")
		if anim_layers:
			for i in anim_layers:
				layers_str += '"' + i + '",'
			oo = "string $layers[]=" + "{" + layers_str[:-1] + "}" + ';'#layerEditorMergeAnimLayer($layers,0);
			mel.eval(oo)
			mel.eval("global string $gSelectedAnimLayers[];$gSelectedAnimLayers = $layers;")
			mel.eval("performAnimLayerMerge( 0 );")
			cmds.delete(cmds.ls(type="animLayer"))'''

    def TPose_Run ( self , ChrName ) :
        self.log = ""
        self.ReferencePaths = cmds.file ( q = 1 , reference = 1 )
        for referencePath in self.ReferencePaths :
            if ChrName in referencePath :
                if cmds.referenceQuery ( referencePath , isLoaded = 1 ) :
                    ReferenceNamespace = cmds.referenceQuery ( referencePath , namespace = 1 )
                    self.Namespace = ReferenceNamespace.split ( ":" ) [ 1 ]
                    try :
                        self.NamespaceCvs_List = [ ]
                        for KeyCv in self.KeyCvs :
                            if cmds.objExists ( self.Namespace + ":" + KeyCv ) :
                                self.NamespaceCvs_List.append ( self.Namespace + ":" + KeyCv )
                        cmds.bakeResults ( self.NamespaceCvs_List , time = (self.starTime - 10 , self.starTime) , simulation = 1 , sampleBy = 1 , oversamplingRate = 1 , preserveOutsideKeys = 1 , sparseAnimCurveBake = 0 , removeBakedAttributeFromLayer = 0 , removeBakedAnimFromLayer = 0 , bakeOnOverrideLayer = 0 , minimizeRotation = 1 , controlPoints = 0 , shape = 1)
                        mins = min ( cmds.keyframe ( self.NamespaceCvs_List , q = 1 ) )
                        if mins < self.copyTime :
                            cmds.cutKey ( self.NamespaceCvs_List , t = (mins , self.copyTime - 1) )
                        cmds.currentTime ( 70 )
                        cmds.setKeyframe ( self.NamespaceCvs_List )
                        cmds.currentTime ( 50 )
                        cmds.setKeyframe ( self.NamespaceCvs_List )
                        cmds.currentTime ( 0 )
                        for CenterLeg in self.NamespaceCvs_List :
                            for attribute in self.Attributes :
                                CenterLegCvName_attribute = (CenterLeg + "." + attribute)
                                if cmds.getAttr ( CenterLegCvName_attribute , lock = 1 ) == 0 :
                                    cmds.setAttr ( CenterLegCvName_attribute , 0 )
                        cmds.setKeyframe ( self.NamespaceCvs_List )
                        self.Locator_list = [ ]
                        self.Locator_dic = { }
                        self.Create_Locator ( self.RootX_M )
                        self.Create_Locator ( self.IK_Limbs )
                        self.Create_Locator ( self.IK_PoleArms )
                        self.Create_Locator ( self.FK_Limbs_Globals )

                        for nun in range ( len ( self.Locator_list ) - 1 ) :
                            cmds.parent ( self.Locator_list [ nun + 1 ] , self.Locator_list [ 0 ] )
                        self.NamespaceCvs_List.remove ( self.Namespace + ":" + self.RootX_M [ 0 ] )
                        cmds.setKeyframe ( self.NamespaceCvs_List )
                        cmds.setKeyframe ( self.NamespaceCvs_List , breakdown = 0 , hierarchy = "none" , controlPoints = 0 , shape = 0 , time = self.TopesTime )
                        cmds.currentTime ( 50 )
                        Get_MoveRotation_RootX_M = cmds.xform ( self.Namespace + ":" + self.RootX_M [ 0 ] , query = 1 , worldSpace = 1 , rotation = 1 )
                        Get_MoveTranslation_RootX_M = cmds.xform ( self.Namespace + ":" + self.RootX_M [ 0 ] , query = 1 , worldSpace = 1 , translation = 1)
                        cmds.xform (self.Locator_list [ 0 ] , p = 1 , worldSpace = 1 , rotation = Get_MoveRotation_RootX_M)
                        cmds.xform (self.Locator_list [ 0 ] , p = 1 , worldSpace = 1 , translation = Get_MoveTranslation_RootX_M)
                        del self.Locator_dic [ self.Locator_list [ 0 ] ]
                        for key , values in self.Locator_dic.items ( ) :
                            for IK_PoleArm in self.IK_PoleArms :
                                if IK_PoleArm in values :
                                    cmds.pointConstraint ( key , values , offset = [ 0 , 0 , 0 ] , weight = 1 )
                            for IK_Limb in self.IK_Limbs :
                                if IK_Limb in values :
                                    cmds.parentConstraint ( key , values , weight = 1 )
                            for FK_Limbs_Global in self.FK_Limbs_Globals :
                                if FK_Limbs_Global in values :
                                    cmds.parentConstraint ( key , values , weight = 1 )
                        cmds.setKeyframe ( self.Locator_dic.values ( ) )
                        cmds.delete ( self.Locator_list [ 0 ] )
                        self.NamespaceCvs_List.append ( self.Namespace + ":" + self.RootX_M [ 0 ] )
                        FilterCv = [ ]
                        for KeyCv in self.NamespaceCvs_List :
                            for attribute in self.Attributes :
                                FilterCv.append ( KeyCv + "." + attribute )
                        cmds.filterCurve ( FilterCv )
                        cmds.keyTangent ( self.NamespaceCvs_List , e = 1 , t = ( self.TopesTime , self.copyTime) , itt = 'auto' , ott = 'auto' )
                        cmds.select ( self.NamespaceCvs_List )
                        cmds.cutKey ( t = (-1 , 1) )
                        cmds.select ( cl = 1 )
                        self.log += self.Namespace + "-YES,"
                    except :
                        self.log += self.Namespace + "-NO,"
                else :
                    ReferenceNamespace = cmds.referenceQuery ( referencePath , filename = 1 , shortName = 1 ).split ( "." ) [ 0 ]
                    self.log += ReferenceNamespace + "-NO,"
        return (self.log)

    def Create_Locator ( self , Create_List ) :
        try :
            for Leg in Create_List :
                for attribute in self.Attributes :
                    if cmds.getAttr ( self.Namespace + ":" + Leg + "." + attribute , lock = 1 ) == 0 :
                        cmds.setAttr ( self.Namespace + ":" + Leg + "." + attribute , 0 )
                world_rotation = cmds.xform ( self.Namespace + ":" + Leg , query = 1 , worldSpace = 1 , rotation = 1 )
                world_translation = cmds.xform ( self.Namespace + ":" + Leg , query = 1 , worldSpace = 1 , translation = 1 )
                RotateOrder = cmds.xform ( self.Namespace + ":" + Leg , q = 1 , roo = 1 )
                # 创建定位器
                self.LocatorName = self.Namespace + Leg + "_Locator"
                dingwei = cmds.spaceLocator ( name = self.LocatorName )
                cmds.xform ( dingwei , p = 1 , roo = RotateOrder )
                cmds.xform ( dingwei , p = 1 , worldSpace = 1 , rotation = world_rotation )
                cmds.xform ( dingwei , p = 1 , worldSpace = 1 , translation = world_translation )
                self.Locator_list.append ( self.LocatorName )
                self.Locator_dic [ self.LocatorName ] = self.Namespace + ":" + Leg
        except :
            locators = cmds.ls ( "*_Locator*" )
            cmds.delete ( locators )